package br.com.traffic.help.converter;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

import br.com.traffic.help.dao.HelpDAO;
import br.com.traffic.help.model.TipoUnidade;


public class TipoUnidadeConverter implements Converter {
	
	 public Object getAsObject(FacesContext facesContext, UIComponent component, String submittedValue) {  
	        if (submittedValue.trim().equals("")) {  
	            return null;  
	        } else {  
	            try {  
	                int number = Integer.parseInt(submittedValue);  
	  
	                return new HelpDAO().buscaTipoUnidadeId(number); 
	  
	            } catch(NumberFormatException exception) {  
	                throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid player"));  
	            }  
	        }  
	    } 
	    
	 
	  public String getAsString(FacesContext facesContext, UIComponent component, Object value) {  
	        if (value == null || value.equals("")) {  
	            return "";  
	        } else {  
	            return String.valueOf(((TipoUnidade) value).getId());  
	        }  
	    } 

}
